using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Common.Database
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class  DbTableStats
    {
        #region fields
        private DbTableKey _TableKey;
        private int _RecordCount;
        private long _DbFileSize;
        private int _RowSize;
        private DateTime _CreationTime;
        private DateTime _ModificationTime;
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public DbTableStats()
        {
            this._TableKey = new DbTableKey();
            this._RecordCount = 0;
            this._DbFileSize = 0;
            this._RowSize = 0;
            this._CreationTime = new DateTime(1900, 1, 1);
            this._ModificationTime = new DateTime(1900, 1, 1);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="recordCount"></param>
        /// <param name="dbFileSize"></param>
        /// <param name="rowSize"></param>
        /// <param name="createTime"></param>
        /// <param name="modifyTime"></param>
        public DbTableStats(DbTableKey key, int recordCount, long dbFileSize, int rowSize, DateTime createTime, DateTime modifyTime)
        {
            this._TableKey = key;
            this._RecordCount = recordCount;
            this._DbFileSize = dbFileSize;
            this._RowSize = rowSize;
            this._CreationTime = createTime;
            this._ModificationTime = modifyTime;
        }
        #endregion

        #region props
        /// <summary></summary>
        public DbTableKey TableKey
        {
            get { return _TableKey; }
            set { _TableKey = value; }
        }
        /// <summary></summary>
        public int RecordCount
        {
            get { return _RecordCount; }
            set { _RecordCount = value; }
        }
        /// <summary></summary>
        public long DbFileSize
        {
            get { return _DbFileSize; }
            set { _DbFileSize = value; }
        }
        /// <summary></summary>
        public int RowSize
        {
            get { return _RowSize; }
            set { _RowSize = value; }
        }
        /// <summary></summary>
        public DateTime CreationTime
        {
            get { return _CreationTime; }
            set { _CreationTime = value; }
        }
        /// <summary></summary>
        public DateTime ModificationTime
        {
            get { return _ModificationTime; }
            set { _ModificationTime = value; }
        }
        #endregion

        #region static methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tblKey"></param>
        /// <returns></returns>
        public static DbTableStats BuildTableStats(DbTableKey tblKey)
        {
           DatabaseTableDescription tblDesc=
                DbUtil.GetTableDescription(tblKey.DbProvider, tblKey.ConnStr, tblKey.SchemaName, tblKey.TblName);
            int recordCount = (int)tblDesc.RecordCount;
            int rowSize = (int) tblDesc.MaxRowSize;
            string  fileSizeStr = tblDesc.DataSize;
            Regex fileSizeRegex = new Regex("(\\d+)");
            Match fileSizeMatch = fileSizeRegex.Match(fileSizeStr);
            long fileSize = 0;
            if(fileSizeMatch.Success)
            {
                fileSize = long.Parse(fileSizeMatch.Groups[1].Value);
            }
            DateTime creationTime = tblDesc.CreationDate;
            DateTime modificationTime = tblDesc.ModificationDate;
            return new DbTableStats(tblKey, recordCount, fileSize, rowSize, creationTime, modificationTime);
        }
        #endregion
    }
}
